#ifndef CPH_MEDIA_COMMON_H
#define CPH_MEDIA_COMMON_H

// 音视频服务状态
enum MediaStatus {
    INVALID,
    INIT,
    RUNNING,
    STOPPED
};

// 视频帧类型
enum VideoFrameType {
    H264,
    YUV,       // I420
    RGB,       // RGBA8888
    H265,
    YUV_FDS,   // YUV FDS
    FRAME_TYPE_MAX
};

// 编码模式
enum RCMode {
    ABR,    // 平均码率
    CRF,    // 画质优先
    CBR,    // 恒定码率
    CAPPED_CRF,
    RC_MODE_MAX
};

// 编码类型
enum EncoderType {
    DEFAULT,
    CPU,
    GPU,
    ENCODE_TYPE_MAX
};

// 编码级别
enum ProfileType {
    BASELINE,
    MAIN,
    HIGH
};

// 分辨率
struct FrameSize {
    uint32_t width = 720;
    uint32_t height = 1280;
};

// 视频配置参数
struct VideoConfigParams {
    EncoderType encoderType = DEFAULT;      // 编码器类型
    uint32_t channel = 0;                   // 硬编码通道
    VideoFrameType videoFrameType = H264;   // 视频格式
    uint32_t frameRate = 30;                // 帧率
    uint32_t bitrate = 3000000;             // 码率
    uint32_t gopSize = 3000;                // I帧间隔
    ProfileType profile = MAIN;             // 编码复杂度
    RCMode rcMode = ABR;                    // 流控模式
    FrameSize physicalResolution;           // 云手机物理分辨率
    FrameSize virtualResolution;            // 图片采集分辨率（不高于物理分辨率）
};

// 视频时延统计扩展参数
struct VideoLatencyExt {
    uint64_t startCaptureTimestamp;
    uint64_t endCaptureTimestamp;
    uint64_t startEncodeTimestamp;
    uint64_t endEncodeTimestamp;
};

// 视频扩展回调参数
struct VideoCallbackExt {
    uint64_t timestamp;     // 当前帧的时间戳
    uint8_t orientation;
    VideoLatencyExt videoLatencyExt;
    uint32_t refreshRate;
};

struct YuvFdsCallbackExt {
    uint32_t fourcc;
    uint32_t width;
    uint32_t height;
    uint32_t numLayers;
    struct {
        uint32_t drmFormat;
        uint32_t offset;
        uint32_t pitch;
    } layers[0x4];
    uint8_t reserve[32];  /* 预留32字节*/
};

struct YuvInfoCallbackExt {
    /**
     * timestamp[0]: 预合成的时间点；2帧的timestamp1[0] - timestamp0[0]为帧间隔时间，用其他的Index也是可以的；帧间隔时间用来判断每秒多少帧
     * timestamp[1]: 合成的时间点, timestamp[1] - timestamp[0]为软合成的时间
     * timestamp[2]: 帧数据从server端往client端发送的时间点，timestamp[2] - timestamp[1]为hwc中处理RGB->YUV/共享内存的时间
     * timestamp[3]: client端接收到帧数据的时间点，timestamp[3] - timestamp[2]为server发送到client接收到的时间
     */
    uint64_t timestamp[4];
    uint64_t frameIndex; /* client端连接到server成功后，frameIndex从0自增，直到断开，server端丢帧也会自增 */
    uint32_t width;
    uint32_t height;
    uint32_t orientation; /* 0-竖屏 1-横屏 2-纵向翻转 3-横向翻转*/
    uint8_t reserve[32];  /* 预留32字节*/
};

// 视频动态参数
struct VideoParams {
    VideoFrameType videoFrameType = H264;   // 视频格式，不支持动态
    uint32_t frameRate = 30;                // 帧率，编码参数，静止画面补帧有效
    uint32_t bitrate = 3000000;             // 码率
    uint32_t gopSize = 3000;                // I帧间隔
    ProfileType profile = MAIN;             // 编码复杂度
    RCMode rcMode = ABR;                    // 流控模式
    FrameSize virtualResolution;            // 图片采集分辨率（不高于物理分辨率）
};

// yuv抓图数据
struct CaptureYuvFrame {
    uint8_t *yuvBuf;
    uint32_t yuvBufSize;
    uint8_t orientation;
    uint32_t width;
    uint32_t height;
};

// 音频类型
enum AudioType {
    OPUS,
    PCM
};

// 音频配置参数
struct AudioConfigParams {
    AudioType audioType = OPUS;
    uint32_t sampleRate = 48 * 1000; // 只有在OPUS格式有效,不支持修改
    uint32_t sampleInterval = 10; // ms，只有在OPUS格式有效
};

// 音频扩展回调参数
struct AudioCallbackExt {
    uint8_t channels = 2;                   // 声道数 1：MONO/2:STEREO(默认)
    uint8_t audioBitDepth = 16;             // 采样深度 8bit/16bit(默认)
    uint32_t sampleRate = 48000;            // 采样率 44100Hz/48000Hz(默认)，只有在OPUS格式有效
    uint32_t sampleInterval = 10;           // 采样间隔 10ms(默认)/20ms，只有在OPUS格式有效
    uint64_t timestamp;                     // 音频数据时间戳 毫秒，UTC时间
};

// 音频配置参数
struct AudioParams {
    uint32_t sampleInterval = 10;            // ms，只有在OPUS格式有效
    uint32_t bitrate = 192000;               // bps，只有在OPUS格式有效
};

// 触控协议
enum  InputType : uint8_t {
    typeA,
    typeB
};

// 触控事件
struct CPHTouchEvent {
    uint8_t pointerID;
    uint8_t action;
    int16_t x;
    int16_t y;
    uint16_t pressure;
} __attribute__((packed));

// 触控传输数据
struct OperationInputData {
    uint8_t orientation;
    InputType inputType;
    uint16_t reserved;
    uint16_t physicalWidth;
    uint16_t physicalHeight;
    CPHTouchEvent inputData;
} __attribute__((packed));

// 键盘输入数据
struct CPHKeyInputData {
    uint16_t keyCode;
    uint16_t action;
} __attribute__((packed));

// 摇杆输入数据
struct CPHAxisInputData {
    uint16_t masterAxis;
    uint16_t secondaryAxis;
    int32_t masterValue;
    int32_t secondaryValue;
} __attribute__((packed));

// 仿真硬件状态
enum VmiDeviceStatus {
    VMI_INVALID,
    VMI_INIT,
    VMI_ENABLED,
    VMI_DISABLED
};

// 仿真硬件类型
enum VmiDeviceType {
    CAMERA,
    MICROPHONE,
    SENSOR,
    LOCATION,
    VIBRATOR,
    DEVICE_MAX
};

// 仿真硬件支持的数据类型
enum DataType {
    VMI_H264,
    VMI_JPEG,
    VMI_I420,
    VMI_PCM,
    VMI_OPUS
};

// 仿真硬件初始化配置
struct VmiConfigParams {
    DataType dataType = VMI_H264;
    uint32_t sensorVersion = 0;
    uint32_t reserved2 = 0;
    uint32_t reserved3 = 0;
};

// 仿真硬件扩展回调参数
struct VmiDeviceCallbackExt {
    uint64_t timestamp = 0;
    VmiDeviceType deviceType = CAMERA;
    uint32_t reserved1 = 0;
    uint32_t reserved2 = 0;
    uint32_t reserved3 = 0;
};

// CPHMediaEngine version 5.0 新增
// 触控数据结构体
struct InputTouch {
    uint8_t pointerID;
    uint8_t orientation;
    uint8_t action;
    uint16_t pressure;
    int16_t touchX;
    int16_t touchY;
    uint16_t physicalWidth;
    uint16_t physicalHeight;
    int32_t touchTime;  // 真机触控时间与上一次ACTION_DOWN时间的偏移量。
    uint8_t reserved[16];
};

// CPHMediaEngine version 5.0 新增
// 键值数据结构体
struct InputKey {
    uint16_t keyCode;
    uint16_t action;
    uint8_t reserved[8];
};

// CPHMediaEngine version 5.0 新增
// 摇杆数据结构体
struct InputAxis {
    uint16_t masterAxis;
    uint16_t secondaryAxis;
    int32_t masterValue;
    int32_t secondaryValue;
    uint8_t reserved[8];
};

// 添加Gnss Measurement定位数据结构体
struct GnssClockPacket {
    uint16_t flags;
    int16_t leapSecond;
    int64_t timeNs;
    double timeUncertaintyNs;
    int64_t fullBiasNs;
    double biasNs;
    double biasUncertaintyNs;
    double driftNsps;
    double driftUncertaintyNsps;
    uint32_t hwClockDiscontinuityCount;
}__attribute__((packed));

struct GnssMeasurementEvent {
    uint32_t flags;
    int16_t svid;
    uint8_t constellation;
    double timeOffsetNs;
    uint32_t state;
    int64_t receivedSvTimeInNs;
    int64_t receivedSvTimeUncertaintyInNs;
    double cN0Dbhz;
    double pseudorangeRateMps;
    double pseudorangeRateUncertaintyMps;
    uint16_t accumulatedDeltaRangeState;
    double accumulatedDeltaRangeM;
    double accumulatedDeltaRangeUncertaintyM;
    float carrierFrequencyHz;
    int64_t carrierCycles;
    double carrierPhase;
    double carrierPhaseUncertainty;
    uint8_t multipathIndicator;
    double snrDb;
}__attribute__((packed));

struct GnssMeasurementPacket {
    GnssClockPacket clock;
    uint8_t count;
    GnssMeasurementEvent measurements[64];
}__attribute__((packed));

#endif//CPH_MEDIA_COMMON_H